package com.yadong.smarthome2.protocol2;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yadong.smarthome2.protocol2.RequestProtocol.Parameter;
import com.yadong.smarthome2.protocol2.ResultProtocol.DeviceInfo.Status;
import com.yadong.smarthome2.protocol2.ResultProtocol.Function.Argument;
import com.yadong.smarthome2.protocol2.ResultProtocol.Result;


public class Utils {
    private static final String ITEM_SPLIT = ",";
    private static final String VALUE_SPLIT = ":";

    public static Argument createArgument(Argument.Builder argumentBuilder,
            String name, String type, String limit) {
        argumentBuilder.clear();
        argumentBuilder.setName(name);
        argumentBuilder.setType(type);
        argumentBuilder.setLimit(limit);
        return argumentBuilder.build();
    }

    public static Argument createArgument(Argument.Builder argumentBuilder,
            String name, String type) {
        argumentBuilder.clear();
        argumentBuilder.setName(name);
        argumentBuilder.setType(type);
        return argumentBuilder.build();
    }

    public static Result createResultWithKeyValue(String key, String value) {
        Result.Builder resultBuilder = Result.newBuilder();
        resultBuilder.setTag("");
        resultBuilder.setTime(-1);
        resultBuilder.addResult(key + ":" + value);
        return resultBuilder.build();
    }

    public static Status createStatsu(Status.Builder statusBuilder,
            String name, String type) {
        statusBuilder.clear();
        statusBuilder.setName(name);
        statusBuilder.setType(type);
        return statusBuilder.build();
    }

    public static List<Parameter> keyValueToParameter(Map<String, String> keyValue) {
        List<Parameter> parameters = new ArrayList<RequestProtocol.Parameter>();
        Parameter.Builder builder = Parameter.newBuilder();
        for (String key : keyValue.keySet()) {
            builder.clear();
            builder.setCommnd(key);
            builder.setArgs(keyValue.get(key));
            parameters.add(builder.build());
        }
        return parameters;
    }
    public static List<Parameter> stringToParameters(String string) {
        List<Parameter> parameters = new ArrayList<Parameter>();
        try {
            String[] splited = string.split(ITEM_SPLIT);
            Parameter.Builder builder = Parameter.newBuilder();
            for(String item : splited) {
                builder.clear();
                String allItems[] = item.split(VALUE_SPLIT);
                builder.setCommnd(allItems[0]);
                builder.setArgs(allItems[1]);
                parameters.add(builder.build());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return parameters;
    }

    public static String parametersToString(List<Parameter> parameters) {
        StringBuilder stringBuilder = new StringBuilder();
        for (Parameter parameter : parameters) {
            stringBuilder.append(parameter.getCommnd());
            stringBuilder.append(VALUE_SPLIT);
            stringBuilder.append(parameter.getArgs());
            stringBuilder.append(ITEM_SPLIT);
        }
        //remove last item split
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        return stringBuilder.toString();
    }

    public static List<Status> stringToStatuses(String string) {
        List<Status> statuses = new ArrayList<Status>();
        try {
            String[] splited = string.split(ITEM_SPLIT);
            Status.Builder builder = Status.newBuilder();
            for(String item : splited) {
                builder.clear();
                String allItems[] = item.split(VALUE_SPLIT);
                builder.setName(allItems[0]);
                builder.setType(allItems[1]);
                builder.setValue(allItems[2]);
                statuses.add(builder.build());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statuses;
    }

    public static String statusesToString(List<Status> statuses) {
        StringBuilder stringBuilder = new StringBuilder();
        for (Status status : statuses) {
            stringBuilder.append(status.getName());
            stringBuilder.append(VALUE_SPLIT);
            stringBuilder.append(status.getType());
            stringBuilder.append(VALUE_SPLIT);
            stringBuilder.append(status.getValue());
            stringBuilder.append(ITEM_SPLIT);
        }
        //remove last item split
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        return stringBuilder.toString();
    }
}
